#ifndef TRANSFORMERCONTENTREGISTPROTOCOL_HPP_3
#define TRANSFORMERCONTENTREGISTPROTOCOL_HPP_3

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

///////////////////////////////////////////////////////////////////////////////////////
/// regist protocol style, just used to reigst content
///
/// protocol style
///
///  name | length | mainMessage | subMessage |  target port  |  result port  |  exist port  |  result  |  path length  |  path     |
/// length|   2    |     1       |     1      |      4        |       4       |       4      |     1    |      2        |  path len |
/// offset|   0    |     2       |     3      |      4        |       8       |      12      |     16   |      17       |    19     |
class transContentRegistProtocol{
	public:
		static void setPacketLength(char* content, int length){
			*((short*)(content)) = (short)length;
		}

		static int getPacketLength(const char* content){
			return *((short*)(content));
		}

		static void setPacketMainMessageCode(char* content, int mainMessageCode){
			content[2] = (char)mainMessageCode;
		}

		static int getPacketMainMessageCode(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?content[0]:content[2];
		}	

		static void setPacketSubMessage(char* content, int subMessageCode){
			content[3] = (char)subMessageCode;
		}

		static int getPacketSubMessage(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?content[1]:content[3];
		}

		static void setPacketTargetPort(char* content, int port){
			*((int*)(content+4)) = port;
		}

		static int getPacketTargetPort(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?*((int*)(content+2)):*((int*)(content+4));
		}

		static void setPacketResultPort(char* content, int port){
			*((int*)(content+8)) = port;
		}

		static int getPacketResultPort(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?*((int*)(content+6)): *((int*)(content+8));
		}

		static void setPacketExistPort(char* content, int port){
			*((int*)(content+12)) = port;
		}

		static int getPacketExistPort(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?*((int*)(content+10)):*((int*)(content+12));
		}

		static int getPacketResult(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?content[14]:content[16];
		}

		static void setPacketResult(char* content, int result){
			content[16] = (char)result;
		}

		static void setPacketPathLen(char* content, int len){
			*((short*)(content+17)) = (short)len;
		}

		static int getPacketPathLen(const char* content, int towards){
			return towards==DUMP_PROTOCOL_PACKET_IN?*((short*)(content+15)):*((short*)(content+17));
		}

		static void setPacketPathValue(char* content, const char* path, int len){
			if(len > 200){
				return;
			}
			memcpy(content+19, path, len);
		}

		static int getPacketPathValue(const char* content, char* path, int len, int towards){
			int _len=getPacketPathLen(content, towards);
			int startIndex = 0;
			if(_len > len){
				return _len;
			}
			startIndex = towards == DUMP_PROTOCOL_PACKET_IN?17:19;
			memset(path, 0x00, len);
			memcpy(path, content+startIndex, _len);
			return _len;
		}

		static void dumpPacket(const char* content, int towards, char* filePath){
			FILE* dumpfile = fopen(filePath, "w+");
			char path[256]={0};
			if(dumpfile == NULL){
				return;
			}
			fprintf(dumpfile, "\t\t\t\tRegist Packet");
			if(towards == DUMP_PROTOCOL_PACKET_OUT)
				fprintf(dumpfile, "\tpacket length: %d", getPacketLength(content));
			fprintf(dumpfile, "\tmain message code: %d", getPacketMainMessageCode(content, towards));
			fprintf(dumpfile, "\tsub message code: %d", getPacketSubMessage(content, towards));
			fprintf(dumpfile, "\ttarget port is: %d", getPacketTargetPort(content, towards));
			fprintf(dumpfile, "\tresult port is: %d", getPacketResultPort(content, towards));
			fprintf(dumpfile, "\texist port is: %d", getPacketExistPort(content, towards));
			fprintf(dumpfile, "\tregist result is: %d", getPacketResult(content, towards));
			fprintf(dumpfile, "\tlocal fifo name length: %d", getPacketPathLen(content, towards));
			getPacketPathValue(content, path, 256, towards); 
			fprintf(dumpfile, "\tlocal fifo name is :%s", path);
		}
};
#endif //TRANSFORMERCONTENTREGISTPROTOCOL_HPP_3
